﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using FarseerGames.FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class FixedAngleJoint : Joint
    {
        protected FarseerGames.FarseerPhysics.Dynamics.Joints.FixedAngleJoint mFixedAngleJoint;        
        
        /// <summary>
        /// Gets or sets the angle in degrees that the RigidBody will try to achieve.
        /// </summary>
        public Single TargetAngle { get { return MathHelper.ToRadians(mFixedAngleJoint.TargetAngle); } set { mFixedAngleJoint.TargetAngle = MathHelper.ToDegrees(value); } }

        /// <summary>
        /// Gets or sets the angle in radians that the RigidBody will try to achieve.
        /// </summary>
        public Single TargetAngleRadians { get { return mFixedAngleJoint.TargetAngle; } set { mFixedAngleJoint.TargetAngle = value; } }

        /// <summary>
        /// Gets or sets the maximum torque the RigidBody will use when attempting to achieve the TargetAngle. 
        /// </summary>
        public Single MaxImpulse { get { return mFixedAngleJoint.MaxImpulse; } set { mFixedAngleJoint.MaxImpulse = value; } }

        /// <summary>
        /// Creates an instance of a FixedAngleJoint component.
        /// </summary>
        /// <param name="gameObject">The GameObject this component is attached to.</param>
        public FixedAngleJoint(GameObject gameObject)
            : base(gameObject)
        {
            if (RigidBody == null)
            {

                mFixedAngleJoint = new FarseerGames.FarseerPhysics.Dynamics.Joints.FixedAngleJoint();
                mJoint = mFixedAngleJoint;
            }
            else
            {
                mFixedAngleJoint = JointFactory.Instance.CreateFixedAngleJoint(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body);
                mJoint = mFixedAngleJoint;
            }
        }

        /// <summary>
        /// Create the Joint after setting initial settings.
        /// </summary>
        public override void Create()
        {
            mFixedAngleJoint = JointFactory.Instance.CreateFixedAngleJoint(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body);
            mJoint = mFixedAngleJoint;
        }
    }
}
